package com.zyd.shiro.business.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.zyd.shiro.business.entity.Categories;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.Category;
import com.zyd.shiro.persistence.dao.MallCategorieDao;
import com.zyd.shiro.persistence.dao.MallCategoryDao;
import com.zyd.shiro.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

/**
 * 服务层
 *
 * @author RookieWZW
 */
@Service
public class MallCategoryService {

    @Autowired
    private MallCategoryDao categoryDao;

    @Autowired
    private MallCategorieDao categoriesDao;

    @Autowired
    private QiniuUtil qiniuUtil;


    public List<Category> findAll() {

        return categoryDao.findAll();

    }

    public List<Categories> findAsMultiLevel() {
        return categoriesDao.findAll(new Specification<Categories>() {
            @Override
            public Predicate toPredicate(Root<Categories> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path id = root.get("parent_id");
                return cb.equal(id, 0);
            }

        });
    }

    public Category findOne(Integer id) {
        Category category = categoryDao.findOne(id);
        String url = null;
        try {
            url = qiniuUtil.getFile(category.getImage_id());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (url != null)
            category.setImage_id(url);
        return category;
    }

    public void add(Category category, MultipartFile file) {
        String name = null;
        if (file != null && file.getSize() > 0) {
            try {
                name = qiniuUtil.putFile(file.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (name != null) {
            category.setImage_id(name);
        }
        categoryDao.save(category);
    }

    public boolean update(Category category, MultipartFile file) {
        if (category.getId() < 1)
            return false;
        String name = null;
        if (file != null && file.getSize() > 0) {
            try {
                name = qiniuUtil.putFile(file.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (name != null) {
            category.setImage_id(name);
        }
        categoryDao.save(category);
        return true;
    }

    public boolean delete(Integer id) {
        List<Category> categoryList = findCategoryParallel(id);
        if (categoryList.size() > 0) {
            return false;
        } else {
            categoryDao.delete(id);
            return true;
        }

    }


    public List<Category> findCategoryParallel(Integer parentId) {

        return categoryDao.findByParent_id(parentId);
    }

    /**
     * 递归查询本节点的id及孩子节点的id
     *
     * @param categoryId
     * @return
     */
    public List<Category> selectCategoryAndChildrenById(Integer categoryId) {
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet, categoryId);

        List<Category> categoryList = Lists.newArrayList();
        if (categoryId != null) {
            for (Category categoryItem : categorySet) {
                categoryList.add(categoryItem);
            }
        }
        return categoryList;
    }


    //递归算法,算出子节点
    private Set<Category> findChildCategory(Set<Category> categorySet, Integer categoryId) {
        Category category = categoryDao.findOne(categoryId);
        if (category != null) {
            categorySet.add(category);
        }
        //查找子节点,递归算法一定要有一个退出的条件
        List<Category> categoryList = categoryDao.findByParent_id(categoryId);
        for (Category categoryItem : categoryList) {
            findChildCategory(categorySet, categoryItem.getId());
        }
        return categorySet;
    }

    public PageInfo findCategoryByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<Category> categories = categoryDao.findAll();
        if (CollectionUtils.isEmpty(categories)) {
            return null;
        }
        for (Category category : categories) {
            String url = null;
            try {
                url = qiniuUtil.getFile(category.getImage_id());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            category.setImage_id(url);
        }
        PageInfo bean = new PageInfo<Category>(categories);
        bean.setList(categories);
        return bean;
    }
}
